package com.example.administrator.kotlin.activity

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import com.example.administrator.kotlin.R
import com.example.administrator.kotlin.fragment.FirstPageFragment
import com.example.administrator.kotlin.fragment.HistoryFragment
import com.example.administrator.kotlin.fragment.NewsFragment
import com.example.administrator.kotlin.fragment.RecommandFragment
import kotlinx.android.synthetic.main.item_viewpager.*
import org.jetbrains.anko.onClick

/**
 * Created by Administrator on 2018/2/28/028.
 */
class MainActivity : AppCompatActivity() {
    var lastIndex = -1
    var lastFragment: Fragment? = null
    var firstPageFragment: FirstPageFragment? = null
    var newsFragment: NewsFragment? = null
    var recommandFragment: RecommandFragment? = null
    var historyFragment: HistoryFragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.item_viewpager)

        changeTab(0)

        iv_tuijian.onClick { changeTab(0) }
        iv_meizhi.onClick { changeTab(1) }
        iv_lishi.onClick { changeTab(2) }
        iv_ceshi.onClick { changeTab(3) }


//        navigation.setOnNavigationItemSelectedListener { item ->
//            when (item.itemId) {
//                R.id.action_recommend -> {
//                    changeTab(0)
//                }
//                R.id.action_girl -> {
//                    changeTab(1)
//                }
//                R.id.action_history -> {
//                    changeTab(2)
//                }
//            }
//
//            true
//
//        }
//
//        changeTab(0)
    }

    private fun changeTab(i: Int) {
        if (lastIndex == i) return
        lastIndex = i

        val fragmentManager = supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        if (lastFragment != null) {
            transaction.hide(lastFragment)
        }

        when (i) {
            0 -> {
                firstPageFragment = fragmentManager.findFragmentByTag(FirstPageFragment::class.java.simpleName) as FirstPageFragment?
                if (firstPageFragment == null) {
                    firstPageFragment = FirstPageFragment.newInstance()
                    transaction.add(R.id.container, firstPageFragment, FirstPageFragment::class.java.simpleName)
                } else {
                    transaction.show(firstPageFragment)
                }
                lastFragment = firstPageFragment
            }
            1 -> {
                newsFragment = fragmentManager.findFragmentByTag(NewsFragment::class.java.simpleName) as NewsFragment?
                if (newsFragment == null) {
                    newsFragment = NewsFragment.newInstance()
                    transaction.add(R.id.container, newsFragment, NewsFragment::class.java.simpleName)
                } else {
                    transaction.show(newsFragment)
                }

                lastFragment = newsFragment
            }
            2 -> {
                recommandFragment = fragmentManager.findFragmentByTag(RecommandFragment::class.java.simpleName) as RecommandFragment?
                if (recommandFragment == null) {
                    recommandFragment = RecommandFragment.newInstance()
                    transaction.add(R.id.container, recommandFragment, RecommandFragment::class.java.simpleName)
                } else {
                    transaction.show(recommandFragment)
                }

                lastFragment = recommandFragment
            }
            3 -> {
                historyFragment = fragmentManager.findFragmentByTag(HistoryFragment::class.java.simpleName) as HistoryFragment?
                if (historyFragment == null) {
                    historyFragment = HistoryFragment.newInstance()
                    transaction.add(R.id.container, historyFragment, HistoryFragment::class.java.simpleName)
                } else {
                    transaction.show(historyFragment)
                }

                lastFragment = historyFragment
            }
        }

        transaction.commit()

    }

//    private fun changeTab(i: Int) {
//        if (lastIndex == i) return
//        lastIndex = i;
//
//        val fragmentManager = supportFragmentManager
//        val transaction = fragmentManager.beginTransaction()
//        if (lastFragment != null) {
//            transaction.hide(lastFragment)
//        }
//
//        when (i) {
//            0 -> {
//                firstPageFragment = fragmentManager.findFragmentByTag(FirstPageFragment::class.java.simpleName) as FirstPageFragment?
//                if (firstPageFragment == null) {
//                    firstPageFragment = FirstPageFragment.newInstance()
//                    transaction.add(R.id.container, firstPageFragment, FirstPageFragment::class.java.simpleName)
//                } else {
//                    transaction.show(firstPageFragment)
//                }
//                lastFragment = firstPageFragment
//            }
//            1 -> {
//                newsFragment = fragmentManager.findFragmentByTag(NewsFragment::class.java.simpleName) as NewsFragment?
//                if (newsFragment == null) {
//                    newsFragment = NewsFragment.newInstance()
//                    transaction.add(R.id.container, newsFragment, NewsFragment::class.java.simpleName)
//                } else {
//                    transaction.show(newsFragment)
//                }
//                lastFragment = newsFragment
//            }
//            2 -> {
//                recommandFragment = fragmentManager.findFragmentByTag(RecommandFragment::class.java.simpleName) as RecommandFragment?
//                if (recommandFragment == null) {
//                    recommandFragment = RecommandFragment.newInstance()
//                    transaction.add(R.id.container, recommandFragment, RecommandFragment::class.java.simpleName)
//                } else {
//                    transaction.show(recommandFragment)
//                }
//                lastFragment = recommandFragment
//            }
//        }
//
//        transaction.commit()
//
//    }
}